package clearsyntax;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

public class GenTuples {

	private static void repeat(StringBuilder b, int number, String... letters) {
		for (int i = 1; i <= number; i++) {
			if (i > 1)
				b.append(", ");
			for (int j = 0; j < letters.length; j++) {
				if (j > 0)
					b.append(" ");
				b.append(letters[j]).append(i);
			}
		}
	}

	public static void generateTuple(int number, String dir) throws IOException {
		StringBuilder b = new StringBuilder();
		b.append("package literals.types;\n\n");
		b.append("public class Tuple").append(number).append("<");
		repeat(b, number, "T");
		b.append("> extends Tuple").append(number - 1).append("<");
		repeat(b, number - 1, "T");
		b.append("> {\n\n\tpublic T").append(number).append(" _")
				.append(number).append(" = null;\n\n\t");
		b.append("public Tuple").append(number).append("(");
		repeat(b, number - 1, "T", "o");
		b.append(") {\n\t\tsuper(");
		repeat(b, number - 1, "o");
		b.append(");\n\t\t_").append(number).append(" = o").append(number)
				.append(";\n\t}\n\n}");
		System.out.println(b);
		Writer writer = new PrintWriter(new File(dir, "Tuple" + number
				+ ".java"));
		writer.write(b.toString());
		writer.flush();
		writer.close();
	}

	public static void generateTupleFunction(int number) {
		StringBuilder b = new StringBuilder();
		b.append("public static <");
		repeat(b, number, "T");
		b.append("> Tuple").append(number).append("<");
		repeat(b, number, "T");
		b.append("> o(");
		repeat(b, number, "T", "o");
		b.append(") {\n");
		b.append("return new Tuple").append(number).append("<");
		repeat(b, number, "T");
		b.append(">(");
		repeat(b, number, "o");
		b.append(");\n}");
		System.out.println(b);
	}

	public static void main(String[] args) throws IOException {
		final String dir = "src/literals/types";
		for (int i = 1; i <= 22; i++) {
			generateTuple(i, dir);
			generateTupleFunction(i);
		}
	}

}
